package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;

public class ResourceFileDemo extends AppCompatActivity {
    Button buttonReadTxt;
    Button buttonReadXML;
    Button buttonClear;
    TextView textViewResult;
    //资源实例
    Resources resources;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resource_file_demo);
        buttonReadTxt = findViewById(R.id.button_read_txt);
        buttonReadXML = findViewById(R.id.button_read_xml);
        buttonClear = findViewById(R.id.button_clear);
        textViewResult = findViewById(R.id.textView_result);
        //获取资源实例
        resources = getResources();
        buttonReadTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    InputStream in = resources.openRawResource(R.raw.raw_file);
                    byte[] data = new byte[in.available()];
                    if(in.read(data) != -1){
                        textViewResult.setText(new String(data));
                    }
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        buttonReadXML.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                XmlResourceParser parser = resources.getXml(R.xml.people);
                String msg = "";
                try {
                    while(parser.next() != XmlResourceParser.END_DOCUMENT){
                        //获得元素的名称
                        String people = parser.getName();
                        String name = null, age = null, height = null;
                        if(people != null && people.equals("person")){
                            //获取元素的属性数量
                            int count = parser.getAttributeCount();
                            for(int i = 0; i < count; i++){
                                //得到属性名称
                                String attributeName = parser.getAttributeName(i);
                                //得到属性值
                                String attributeValue = parser.getAttributeValue(i);
                                if(attributeName != null){
                                    if(attributeName.equals("name")){
                                        name = attributeValue;
                                    }else if(attributeName.equals("age")){
                                        age = attributeValue;
                                    }else if(attributeName.equals("height")){
                                        height = attributeValue;
                                    }
                                }
                                if(name != null && age != null && height != null){
                                    msg += "姓名："+name+"，年龄："+age+"，身高："+height+"\n";
                                }
                            }
                        }
                    }
                    textViewResult.setText(msg);
                } catch (IOException | XmlPullParserException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        buttonClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textViewResult.setText("");
            }
        });
    }
}